﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using VehicleManagement.Models;
using VehicleManagement.ControllersServices;
using VehicleManagement.Codes;
using System.Data.Linq;

namespace VehicleManagement.WebService
{
    /// <summary>
    /// WebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [SoapRpcService]
    //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    //[System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public bool Login(string driverLoginName,string password)
        {
            DriverServices ds = new DriverServices();
            return ds.DriverLogin(driverLoginName, password);
        }

        [WebMethod]
        public Track GetTrack(string driverLoginName)
        {
            DriverServices ds = new DriverServices();
            int id = ds.GetDriverIdByLoginName(driverLoginName);
            Table<Track> tracks = VehicleManagementDataContextManager.Instance.GetTable<Track>();
            Track track = tracks.Single(t => t.DriverID == id && t.State == 0);
            return track;
        }

        [WebMethod]
        public bool SubmitPoints(List<TrackPoint> points)
        {
            VehicleManagementDataContext context = VehicleManagementDataContextManager.Instance;
            Table<TrackPoint> trackPoints = context.GetTable<TrackPoint>();
            foreach (TrackPoint point in points)
            {
                point.TrackPointID = Guid.NewGuid();
            }
            try
            {
                trackPoints.InsertAllOnSubmit(points);
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        [WebMethod]
        public bool UpdateTrack(int id,float timeCost,int state)
        {
            VehicleManagementDataContext context = VehicleManagementDataContextManager.Instance;
            Track track = context.GetTable<Track>().Single(t => t.TrackID == id);
            track.TimeCost = timeCost;
            track.State = state;
            try
            {
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
